package com.usooft.metaTalent.core.platform.service.base;

import com.usooft.framework.core.query.Limit;
import com.usooft.framework.core.query.Page;
import com.usooft.metaTalent.core.platform.entity.Expert;
import com.usooft.metaTalent.core.platform.mapper.ExpertMapper;
import com.usooft.metaTalent.core.platform.query.ExpertQuery;
import com.usooft.metaTalent.core.platform.service.ExpertService;
import com.usooft.metaTalent.core.platform.wrapper.ExpertWrapper;
import java.lang.Long;
import java.lang.Override;
import java.lang.String;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.lang.NonNull;

public abstract class BaseExpertService implements ExpertService {
    @Autowired
    private ExpertMapper expertMapper;

    @Override
    public Expert get(@NonNull Long id) {
        return find(id).orElseThrow();
    }

    @Override
    public Optional<Expert> find(@NonNull Long id) {
        return Optional.ofNullable(expertMapper.get(id));
    }

    @Override
    public List<ExpertWrapper> list(ExpertQuery query) {
        return expertMapper.list(query);
    }

    @Override
    public Page<ExpertWrapper> page(ExpertQuery query) {
        int rows = expertMapper.count(query.getFilter());
        return new Page<>(rows, rows == 0 ? new ArrayList<>() : expertMapper.list(query));
    }

    protected Optional<Expert> findOne(ExpertQuery.Filter filter, ExpertQuery.Sorts sorts) {
        List<ExpertWrapper> wrappers = list(ExpertQuery.builder()
                                .filter(filter)
                                .sorts(sorts)
                                .limit(Limit.rowCount(1))
                                .build());
        return wrappers.stream().findFirst().map(Function.identity());
    }

    protected Optional<Expert> findOne(ExpertQuery.Filter filter) {
        return findOne(filter, ExpertQuery.Sorts.builder().build());
    }

    @Override
    public int count(ExpertQuery.Filter filter) {
        return expertMapper.count(filter);
    }

    protected Expert internalAdd(String name, String title, String pictureUrl) {
        Expert expert = new Expert();
        expert.setName(name);
        expert.setTitle(title);
        expert.setPictureUrl(pictureUrl);

        expert.setCreatedDate(LocalDateTime.now());
        expert.setVersion(0);
        expertMapper.add(expert);
        return expert;
    }

    protected void internalUpdate(@NonNull Expert expert) {
        expert.setLastModifiedDate(LocalDateTime.now());

        int rows = expertMapper.update(expert);
        if (rows == 0) {
            throw new OptimisticLockingFailureException("更新失败。可能专家 (" + expert.getId() + ") 的版本 (" + expert.getVersion() + ") 已不是最新");
        }
        expert.setVersion(expert.getVersion() + 1);
    }
}
